#pragma once

#define AES_KEY "\"www.veno2.com\""
#define  AES_align_size(a)  (((a) % 16) == 0 ? 1 : 0)
#define  AES_pad_align_size(a)  ((a)  % 16) ? (((a)  >> 4)  + 1) * 16 : (a);
#define MIN(a, b) ((a) > (b) ? (b) : (a))

#define AUTH_ERROR 0
#define AUTH_REJECT 2
#define AUTH_SUCCESS 1 
#define AUTH_ERR_LOAD_CERT -1
#define AUTH_ERR_CONNECT  -2
#define AUTH_ERR_BAD_CERT -3
#define AUTH_ERR_SERVER_NOREPLY  -4
#define AUTH_ERR_PARSE_SERVER_PACKET  -5
#define AUTH_ERR_AES_CONNECT -6

#define ACCT_SUCCESS 1
#define ACCT_ERR_SERVER_NOREPLY  -1
#define ACCT_ERR_SERVER_KICKOFF -2

#define UDP_AUTH_RESPONSE  0
#define UDP_AUTH_REPLY_RESPONSE  1
#define UDP_AUTH_REJECT   2
#define UDP_AUTH_REPLY_REJECT 3
#define UDP_ACCT_KEEPALIVE    10
#define UDP_ACCT_KEEPALIVE_RESPONSE   11
#define UDP_CLIENT_EXIT        12
#define UDP_CLIENT_EXIT_RESPONSE     13

#define CLIENT_HELLO_MESSAGE "myclient1.0"

#define MD5_LENGTH 16
#define SESSION_ID_LENGTH 16
#define USER_NAME_LENGTH 64
#define USER_PASS_LENGTH 32
#define USER_PASSID_LENGTH 22
#define USER_MAC_LENGTH 32
#define UDP_PACKET_SEND_INTERVAL 7000
#define UDP_PACKET_RESEND_INTERVAL 3000
#define UDP_PACKET_RESEND_COUNT 10
#define UDP_PACKET_SECRET_KEY_LENGTH  8
#define UDP_PACKET_MAX_LENGTH  1024
#define CLIENT_RECONNECT_INTERVAL 30000
#define CLIENT_RECONNECT_TIMES 2


#define SERVER_DEFAULT_UDP_PORT "9999"
#define SERVER_DEFAULT_TCP_PORT "4422"

#define BIND_CLIENT_UDP_PORT 0
#define CLINET_UDP_PORT "888"

typedef enum packet_type
{
    SERVER_AUTH_REPLY = 0,
    SERVER_AUTH_MESSAGE_REPLY = 1,
    SERVER_AUTH_REJECT = 2,
	CLIENT_AUTH_REQUEST,
	CLIENT_REAUTH_REQUEST,
    CLIENT_KEEPALIVE = 10,
    SERVER_KEEPALIVE,
	SERVER_SETRATE,
	SERVER_KICKOFF,
    CLIENT_EXIT,
    SERVER_EXIT,
    CLIENT_QUERY,
    SERVER_QUERY,
	ADMIN_AUTH_REQUEST = 30,
	ADMIN_AUTH_ACCEPT,
	ADMIN_AUTH_REJECT,
} PACKET_TYPE;

#define CURRENT_CLIENT_VERSION 7230

/* packet content between server and client is defined here */
# pragma pack (1)

struct udp_packet_keepalive_client
{
	char type;
	unsigned int packet_id;
	unsigned int table_id;
	unsigned int instant_up_rate;
	unsigned int instant_down_rate;
	unsigned int total_up_traffic;
	unsigned int total_down_traffic;
};

struct udp_packet_keepalive_server
{
	char type;
	unsigned int packet_id;
	unsigned int up_rate;
	unsigned int down_rate;
	unsigned short msg_length;
};

struct login_reply_server 
{
	char type;
	unsigned int table_id;
	char secret[UDP_PACKET_SECRET_KEY_LENGTH];
	char user_type;
	unsigned int up_rate;
	unsigned int down_rate;
	unsigned short msg_length;
};

struct login_request_client
{
	int version;
	char username[USER_NAME_LENGTH];
	char password[USER_PASS_LENGTH];
	char mac[USER_MAC_LENGTH];
	int type;
	char secret[20];   // used for relogin
	int radversion;
	char passid[USER_PASSID_LENGTH];
};

# pragma pack ()

int udp_packet_calculate_md5(char *packet_buffer, int packet_size, 
	char *secret, int secret_length, char *md5_out);
int AES_read(int sock, char *buffer, int size,struct aes_ctx * paes);
int AES_write(int sock, char *buffer, int size,struct aes_ctx * paes);
void fr_md5_calc(unsigned char *output, const unsigned char *input,
				 unsigned int inlen);
LPSTR PrintError(int ErrorCode);


